package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.OptionPO;
import com.ajmd.onlineexam.entity.QuestionPO;
import com.ajmd.onlineexam.entity.TestPaperPO;
import com.ajmd.onlineexam.entity.TestPaperQuestionPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.service.QuestionService;
import com.ajmd.onlineexam.service.TestPaperQuestionService;
import com.ajmd.onlineexam.vo.QuestionVO;
import com.ajmd.onlineexam.vo.TestPaperQuestionVO;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ajmd
 * @since 2023/4/28 15:13
 */
@Api("试卷试题接口")
@RestController
@RequestMapping("/testPaperQuestion")
public class TestPaperQuestionController {
    @Resource
    private TestPaperQuestionService tPQuestionService;
    @Resource
    private QuestionService questionService;

    @GetMapping("/select")
    public CommonResult findQuestions(@RequestParam String testPaperId){
        if(ObjectUtils.isEmpty(testPaperId)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        List<TestPaperQuestionVO> tPQuestionVOList = tPQuestionService.getTestPaperQuestion(testPaperId);
//        if(ObjectUtils.isEmpty(tPQuestionVOList)){
//            return CommonResult.error()
//        }
        return CommonResult.ok().data("tPQuestionVOList",tPQuestionVOList);
    }

    @PostMapping("/save")
    public CommonResult saveTestPaperQuestion(@RequestParam String testPaperId,
                @RequestParam BigDecimal maxScore,@RequestParam int totalCount,@RequestParam int radioCount,
                @RequestParam int multipleChoiceCount,@RequestParam int trueOrFalseCount,
                 @RequestParam int fillCount,@RequestParam int questionCount,
                @RequestBody List<TestPaperQuestionPO> testPaperQuestionPOS){

        TestPaperPO testPaperPO = new TestPaperPO();
        testPaperPO.setId(testPaperId);
        testPaperPO.setMaxScore(maxScore);
        testPaperPO.setTotalCount(totalCount);
        testPaperPO.setRadioCount(radioCount);
        testPaperPO.setMultipleChoiceCount(multipleChoiceCount);
        testPaperPO.setTrueOrFalseCount(trueOrFalseCount);
        testPaperPO.setFillCount(fillCount);
        testPaperPO.setQuestionCount(questionCount);

        int num = tPQuestionService.save(testPaperPO,testPaperQuestionPOS);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.TEST_PAPER_QUESTION_SAVE_ERROR);
        }
        return CommonResult.ok().message("保存试卷成功");
    }
}
